package com.neuedu.qian.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.qian.entity.ClassInfo;
import com.neuedu.qian.mapper.ClassInfoMapper;
import com.neuedu.qian.service.IClassInfoService;
import com.neuedu.qian.vo.ResponseData;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("classInfoService")
public class ClassInfoServiceImpl extends ServiceImpl<ClassInfoMapper, ClassInfo> implements IClassInfoService {

    @Override
    public ResponseData insertOne(ClassInfo c) {
        //验证数据
        String college = c.getCollege();
        if (StrUtil.isEmpty(college)) {
            return ResponseData.createFail("请输入班级所属学院名称");
        }
        String classNo = c.getClassNo();
        if (StrUtil.isEmpty(classNo)) {
            return ResponseData.createFail("请输入班级编号");
        }
        String className = c.getClassName();
        if (StrUtil.isEmpty(className)) {
            return ResponseData.createFail("请输入班级名称");
        }
        //检查班级编号是否已存在  假设当前班级编号为:CP0120020220021
        QueryWrapper<ClassInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_no", classNo);
        //全表统计查询
        long count = getBaseMapper().selectCount(queryWrapper);
        if (count > 0) {
            return ResponseData.createFail("班级编号已存在");
        }
        c.setCreateTime(new Date());
        c.setRecordStatus(1);
        //返回影响行数
        int rows = getBaseMapper().insert(c);
        return ResponseData.success(rows);
    }

    @Override
    public List<ClassInfo> selectList(String keyword, ClassInfo c) {
        QueryWrapper<ClassInfo> queryWrapper = new QueryWrapper<>();
        //设置查询条件
        if (StrUtil.isNotEmpty(keyword)) {
            queryWrapper.and(wrapper -> wrapper.like("college", keyword)
                    .or().like("class_no", keyword)
                    .or().like("class_name", keyword));
        }
        if (c == null) {
            return getBaseMapper().selectList(queryWrapper);
        }
        //记录状态
        Integer recordStatus = c.getRecordStatus();
        if (recordStatus != null) {
            queryWrapper.eq("record_status", recordStatus);
        }
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public ResponseData updateOne(ClassInfo c) {
        if (c == null) {
            return ResponseData.modifyFail("未填写任何数据");
        }
        Integer classId = c.getClassId();
        if (classId == null) {
            return ResponseData.modifyFail("未填写班级ID");
        }
        String classNo = c.getClassNo();
        if (StrUtil.isNotEmpty(classNo)) {
            return ResponseData.modifyFail("班级编号不可以修改");
        }
        Integer recordStatus = c.getRecordStatus();
        if (!(recordStatus == null || 1 == recordStatus || 9 == recordStatus)) {
            return ResponseData.modifyFail("班级记录状态错误");
        }
        c.setUpdateTime(new Date());
        c.setCreateTime(null);
        c.setCreateUserId(null);
        int rows = getBaseMapper().updateById(c);
        return ResponseData.success(rows);
    }
}